<?php
/**
 * User: 登陆模块
 * DateTime: 18-8-3下午3:56
 */

namespace AdminBundle\Controller;

use AdminBundle\Services\s_admin_auth;
use RedUnicorn\Validate\Validate;
use Symfony\Component\HttpFoundation\JsonResponse;

class AuthController extends AdminController
{
    /**
     * 入口
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function indexAction()
    {
        if($this->inlet($this->getParameter('return_page'), true) !==  true){
            return $this->render('@Admin/admin/auth/login.html.twig');
        }

        //TODO return $this->goRoute('在这配置登陆后的第一个页面')

        return $this->render('@Admin/adminFramework/base/index.html.twig');
    }

    /**
     * 登陆
     *
     * @return bool|JsonResponse
     */
    public function loginAction()
    {
        $r = $this->inlet('data', false);
        if($r !== true){
            return $r;
        }

        $s_admin_auth = new s_admin_auth($this->container);
        $data = self::$request->request->all();

        $res = $s_admin_auth->loginAccountPwd($data['account'], $data['password']);
        if ($res['errorCode'] == 0) {
            $res['data']['go_url'] = $this->getUrl('admin_index');
        }

        return new JsonResponse($res);
    }

    /**
     * 退出登陆
     *
     * @return bool|\Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function logoutAction()
    {
        $r = $this->inlet('data');
        if($r !== true){
            return $r;
        }

        $this->removeUserTag();

        return $this->goRoute('admin_index');
    }

    /**
     * 密码修改页面
     *
     * @return bool|\Symfony\Component\HttpFoundation\Response
     */
    public function editPwdPageAction()
    {
        $r = $this->inlet('page');
        if ($r !== true) {
            return $r;
        }

        return $this->render('@Admin/admin/auth/editPwd.html.twig');
    }

    /**
     * 密码修改
     *
     * @return bool|JsonResponse
     */
    public function editPwdAction()
    {
        $r = $this->inlet('data');
        if ($r !== true) {
            return $r;
        }
        $s_admin_auth = new s_admin_auth($this->container);

        $oldPassword = self::$request->request->get('oldPassword');
        $new_password = self::$request->request->get('new_password');
        $ensure_password = self::$request->request->get('ensure_password');
        $user_id = $this->getUserId();

        //数据验证
        //1.基本格式验证
        if (!Validate::isPassword($oldPassword)){
            self::$error->setError('原始密码格式不正确');
        }

        if (!Validate::isPassword($new_password)){
            self::$error->setError('新密码格式不正确');
        }

        if ($ensure_password != $new_password){
            return $this->response('新密码两次输入不匹配');
        }

        //2.旧密码验证
        if (!$s_admin_auth->checkPWD($new_password,$oldPassword,$user_id)){
            return $this->response(self::$error->getError());
        }

        //设置密码
        if (!$s_admin_auth->editPasswordByUserId($new_password,$user_id)){
            return $this->response(self::$error->getError());
        }

        $this->removeUserTag();

        return $this->response('操作成功',0);
    }


}